package com.commute.android;


import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.commute.CommuteEvent;
import com.commute.planner.RecommendedTrip;

public class ListActivity extends Activity {
	
	int nrOfBuses;
	String[][] values;
	
	public static CommuteEvent comeve;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		
		comeve = MainActivity.comeve;
		
		
		
		Intent intent = getIntent();
		TextView event = (TextView) findViewById(R.id.label_upcoming_event);
		event.setText(intent.getStringExtra("EXTRA_EVENT_TITLE") + "\n" + intent.getStringExtra("EXTRA_EVENT_START") + "\n" + intent.getStringExtra("EXTRA_EVENT_LOCATION"));
		
		event.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), EventActivity.class);
				Bundle extras = getIntent().getExtras();
				intent.putExtras(extras);
				startActivity(intent);
			}
		});
		
		ListView listView = (ListView) findViewById(R.id.list_of_busses);
		
		
		
		//int nrOfBuses = 3;
		//String[][] values = new String[nrOfBuses][3];
		
		/*values[0][0] = "Bus 4";
		values[0][1] = "09:30";
		values[0][2] = "09:40";
		values[0][3] = "Smedjegatan";
		values[0][4] = "Universitetsentrén";
		
		values[1][0] = "Bus 5";
		values[1][1] = "09:50";
		values[1][2] = "10:20";
		values[1][3] = "Kyrkbyn";
		values[1][4] = "Smedjegatan";
		values[1][5] = "true";
		values[1][6] = "Bus 7";
		values[1][7] = "10:00";
		values[1][8] = "10:10";
		values[1][9] = "Smedjegatan";
		values[1][10] = "Porsö centrum";
		
		values[2][0] = "Bus 8";
		values[2][1] = "10:15";
		values[2][2] = "10:25";
		values[2][3] = "Smedjegatan";
		values[2][4] = "Porsö centrum";
		
		
	*/
		
		/*String[] ovalues = new String [] {
				"Bus 1" + "\n" + "1" + " from " + "Hoho"
		};*/
		
		
		int nr = comeve.getRecommendedTrips().size();
		String[] ovalues = new String[nr];
		
		/*String stopBusRouteName = "Bus 13";
		String stopStartName = comeve.getRecommendedTrips().get(0).bestStart.getStopName();
		SimpleDateFormat format = new SimpleDateFormat("MMM dd HH:mm");
        String stopStartTime = format.format(comeve.getRecommendedTrips().get(0).startTime);
		ovalues[0] = stopBusRouteName + " \n" + stopStartTime + " from " + stopStartName;*/
		
		//System.out.println("SIZE " + nr + "");
		//android.util.Log.println(1, "SIZE " , nr + "");
		

		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		int counter = 0;
		
		for (RecommendedTrip trip : comeve.getRecommendedTrips()) {
			
			if (trip.isTransfer()) {
				String stopBusRouteNameStart = "Bus " + trip.routeStart.getRouteId();
				String stopBusRouteNameEnd = "Bus " + trip.routeEnd.getRouteId();
				
				String stopStartNameStart = trip.bestStart.getStopName();
				String stopStartNameEnd = trip.bestEnd.getStopName();
				
				String stopStartTime = format.format(trip.startTime);
				String stopEndTime = format.format(trip.endTime);
		        
		        ovalues[counter] = stopBusRouteNameStart + " to " + stopBusRouteNameEnd + "\nFrom " + stopStartNameStart + " " + stopStartTime
		        		+ "\nTo " + stopStartNameEnd + " " + stopEndTime;
		        
				counter++;
			} else {
				String stopBusRouteName = "Bus " + trip.routeEnd.getRouteId();
				String stopStartName = trip.bestStart.getStopName();
				String stopEndName = trip.bestEnd.getStopName();
				String stopStartTime = format.format(trip.startTime);
				String stopEndTime = format.format(trip.endTime);
		        
		        ovalues[counter] = stopBusRouteName + " \nFrom " + stopStartName + " " + stopStartTime
		        		+ "\nTo " + stopEndName + " " + stopEndTime;
				counter++;
			}
		}

		// First parameter - Context
		// Second parameter - Layout for the row
		// Third parameter - ID of the TextView to which the data is written
		// Forth - the Array of data
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, ovalues);

		// Assign adapter to ListView
		listView.setAdapter(adapter); 
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			  @Override
			  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				  SimpleDateFormat format = new SimpleDateFormat("HH:mm");
				  
				  if(comeve.getRecommendedTrips().get(position).isTransfer()){

					  Intent intent = new Intent(getApplicationContext(), ItemTransferActivity.class);
					  
					  String startTime = format.format(comeve.getRecommendedTrips().get(position).startTime);
					  String endTime = format.format(comeve.getRecommendedTrips().get(position).endTime);
					  
					  String waitingTime = format.format(comeve.getRecommendedTrips().get(position).startTime);
					  
					  String startTimeTrans = format.format(comeve.getRecommendedTrips().get(position).startTime);
					  String endTimeTrans = format.format(comeve.getRecommendedTrips().get(position).endTime);
					  
					  intent.putExtra("EXTRA_BUS_ROUTE", comeve.getRecommendedTrips().get(position).routeStart.getRouteId() + "");
					  intent.putExtra("EXTRA_DEPARTURE", startTime + "");
					  intent.putExtra("EXTRA_ARRIVAL", endTime + "");
					  intent.putExtra("EXTRA_FROM", comeve.getRecommendedTrips().get(position).bestStart.getStopName() + "");
					  intent.putExtra("EXTRA_TO", "Smedjegatan");
					  
					  intent.putExtra("EXTRA_BUS_ROUTE_TRANSFER", comeve.getRecommendedTrips().get(position).routeEnd.getRouteId() + "");
					  intent.putExtra("EXTRA_DEPARTURE_TRANSFER", startTime + "");
					  intent.putExtra("EXTRA_ARRIVAL_TRANSFER", endTime + "");
					  intent.putExtra("EXTRA_FROM_TRANSFER", "Smedjegatan");
					  intent.putExtra("EXTRA_TO_TRANSFER", comeve.getRecommendedTrips().get(position).bestEnd.getStopName() + "");
					  
					  
					  startActivity(intent);
					  
					  
				  } else {
					  Intent intent = new Intent(getApplicationContext(), ItemActivity.class);
					  
					  String startTime = format.format(comeve.getRecommendedTrips().get(position).startTime);
					  String endTime = format.format(comeve.getRecommendedTrips().get(position).endTime);
					  
					  intent.putExtra("EXTRA_BUS_ROUTE", comeve.getRecommendedTrips().get(position).routeEnd.getRouteId() + "");
					  intent.putExtra("EXTRA_DEPARTURE", startTime + "");
					  intent.putExtra("EXTRA_ARRIVAL", endTime + "");
					  intent.putExtra("EXTRA_FROM", comeve.getRecommendedTrips().get(position).bestStart.getStopName() + "");
					  intent.putExtra("EXTRA_TO", comeve.getRecommendedTrips().get(position).bestEnd.getStopName() + "");
					  startActivity(intent);
				  }
				  
				
				  
			  }
			}); 
		
	}
	
	private String makeListEvent (String line, String arrival, String event) {
		
		return line + "\t" + arrival + "\n" + event;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_list, menu);
		return true;
	}

}


